<?php

namespace app\admin\model;
use app\admin\model\FreightTeplateArea as fta;
use think\Model;

/**
 * FreightTeplate
 */
class FreightTeplate extends Model
{
    // 表名
    protected $name = 'freight_teplate';

    // 自动写入时间戳字段 
    protected $autoWriteTimestamp = true;


      protected static function onAfterInsert($model)
    {   
          $guan_id = $model->getData("id");
          $area = $model->area;
          foreach($area as &$v){
              $arr = [];
              $arr['first_num'] = $v['shou'];
              $arr['first_price'] = $v['shou_price'];
              $arr['second_num'] = $v['xu'];
              $arr['second_price'] = $v['xu_price'];
              $arr['guan_id'] = $guan_id;
              $arr['all_area_name'] = $v['area_v'];
              $arr['all_area_show_name'] = $v['area'];
              $arr['shop_id'] = 0;
              fta::insert($arr);
          }
          //
    }

    protected static function onAfterUpdate($model){
            $area = $model->area;
            $guan_id = $model->getData("id");
            foreach($area as &$v){
                    if(isset($v['id'])){
                          $arr = [];
                          $arr['first_num'] = $v['shou'];
                          $arr['first_price'] = $v['shou_price'];
                          $arr['second_num'] = $v['xu'];
                          $arr['second_price'] = $v['xu_price'];
                          $arr['all_area_name'] = $v['area_v'];
                          $arr['all_area_show_name'] = $v['area'];
                          fta::where("id",$v['id'])->update($arr);     
                    }else{
                          $arr = [];
                          $arr['first_num'] = $v['shou'];
                          $arr['first_price'] = $v['shou_price'];
                          $arr['second_num'] = $v['xu'];
                          $arr['second_price'] = $v['xu_price'];
                          $arr['guan_id'] = $guan_id;
                          $arr['all_area_name'] = $v['area_v'];
                          $arr['all_area_show_name'] = $v['area'];
                          $arr['shop_id'] = 0;
                          fta::insert($arr);
                    }     
           } 

    }

}

